from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from areas.models import Area
from areas.serializers import AreaSerializer, SubAreaSerializer


class ProvienceAPIView(APIView):

    def get(self,request):

        areas = Area.objects.filter(parent = None)
        serializer = AreaSerializer(areas,many=True)

        return Response(serializer.data)


class CityAPIView(APIView):

    def get(self,request,pk):

        areas = Area.objects.filter(parent =pk)
        serializer = AreaSerializer(areas, many=True)

        return Response(serializer.data)



from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin


class AreaModelViewSet(CacheResponseMixin,ReadOnlyModelViewSet):

    pagination_class = None

    def get_queryset(self):

        if self.action =='list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):

        if self.action =='list':
            return AreaSerializer
        else:
            return SubAreaSerializer